

package TagServer::Model;


#UML_MODELER_BEGIN_PERSONAL_VARS_Model

#UML_MODELER_END_PERSONAL_VARS_Model

#use TagServer::iModel;
use TagClient::View;
#use DBI;

use base qw( TagServer::iModel );



=head1 MODEL



=cut


=head1 PUBLIC ATTRIBUTES

=pod 



=cut


=head1 PUBLIC METHODS

=head4 ->new()

   Return : 
	The class instance      

   Description : 
      The constructor

=cut

sub new
{
  warn "Construtor: Sou o Model\n";
  my $this = shift;
  my $class = ref($this) || $this; 
  my $self = {
  _observer => undef,
  _dataHandle => undef,
  _data => undef,
  _sql => undef,
  };
  bless ($self, $class); 
  $self->dataHandle;
  return $self;
}

=head3 Model->dataHandle($new_handle) 

   Parameters :
      new_handle : $ : the handle to DBMS

   Return : 
      $ : the handle

   Description : 
     this is a basic interface to any DBMS by the DBI.pm
     must be linked to Portal->dataHandle

=cut

sub dataHandle {
   
   warn "Metodo Model->dataHandle\n";
   my $self = shift;
   if (@_) { $self->{_dataHandle} = shift }
   elsif (!$self->{_dataHandle}){
	# INIT DB FIXME this cant be there !!!!
	# Os models devem estar em
	my $dsn = 'dbi:mysql:tagfashion:192.168.1.102';
	my $user = 'filipo' ;
	my $password =  '111111';
	my $h = DBI->connect( $dsn, $user, $password, { RaiseError => 1, AutoCommit => 0}) or die ('Ooops! Can\'t connect!!\n $DBI::errstr\n');
	warn "Criando o Handle";
	my $h = DBI->connect( $dsn, $user, $password);
	$self->{_dataHandle} = $h;
   }
   return $self->{_dataHandle};
}




=pod 

=head3 Model->observer

 the view that I am abserving


=cut

sub observer {
   my $self = shift;
   if (@_) { $self->{_observer} = shift }
   warn "Metodo Model->observer\n";
   return $self->{_observer};
}


=head3 notify

   Parameters :
      observer : TagClient::iView : the view observer

   Return : 
      

   Description : 
      Notify the view observer when this updates.

=cut

sub notify
{
   warn "Metodo Model->notify\n";
   my $self = shift;
   if (@_) { $self->{_observer} = shift }
   $self->observer->show;
   return $self->{_observer};

}

=head3 update

   Parameters :
      new_data : %  the new data to put in this object

   Return : % the data in this object
      

   Description : 
      If parameter new_data exists then receive that data
      Commit the $self->data to DBMS

=cut

sub update
{
   warn "Metodo Model->update\n";
   my $self = shift;
   if (@_) { $self->{_data} = shift }

   if ($self->observer){$self->notify};
   return $self->{_data};
}

=head3 select

   Parameters :
      observer : TagClient::iView : the view observer

   Return : 
      

   Description : 
      Notify the view observer when this updates.

=cut

sub select
{
   warn "Metodo Model->notify\n";
   my $self = shift;
   if (@_) { $self->{_observer} = shift }
   #$self->com->sql;
   return $self->{_observer};

}



=head1 METHODS FOR SUBCLASSING



=cut


=head1 PRIVATE METHODS



=cut






return 1;
